﻿using System.Linq;
using netCoreStudy.Dto.User;
using netCoreStudy.Entities;
using netCoreStudy.Entities.Base;

namespace netCoreStudy.Profile
{
	public class UserProfile : BaseProfile<User, UserDto, AddUserDto, UpdateUserDto>
	{
		public UserProfile()
		{
			Add2Entity
				.ForMember(d => d.MaterialId, o => o.MapFrom(s => s.MaterialId == 0 ? 1 : s.MaterialId))
				.ForMember(d => d.UserRoles, o => o.MapFrom(s => s.RoleIds.Select(x => new UserRole {RoleId = x})))
				;
			Update2Entity
				.ForMember(d => d.UserRoles, o => o.MapFrom(s => s.RoleIds.Select(x => new UserRole() {RoleId = x})))
				;
			Entity2Dto
				.ForMember(d => d.Roles, o => o.MapFrom(s => s.UserRoles))
				.ForMember(d => d.RoleNames, o => o.MapFrom(s => s.UserRoles.Select(x => x.Role.Name).ToArray()))
				.ForMember(d => d.Avatar, o => o.MapFrom(s => $"{s.Material.RealName}.{s.Material.Suffix}"))
				;
		}
	}
}